import { Table, CustomEnum, ListPageTableColumn } from 'custom/index'
import { TableColumnOption } from 'custom/custom/TableColumn'
import ListPageDrawerColumn from '@/components/list-page/ListPageDrawerColumn.vue'

export default class PageTable extends Table<ListPageTableColumn> {
  public static name: CustomEnum = 'PageTable'

  protected static emits = {
    sortChange: 'sortChange',
    selectionChange: 'selectionChange',
    update: 'getData'
  }

  protected slots: { [key in CustomEnum]?: string } = {
    ListPageAuthColumn: 'auth'
  }

  setProp(prop: string, value: any) {
    this._props[prop] = value
    return this
  }
  // 添加标准列
  useColumn(prop: string | TableColumnOption = {}) {
    const option: TableColumnOption = typeof prop === 'string' ? { prop } : prop
    const column = new ListPageTableColumn(option.prop)
    Object.assign(column._props, { minWidth: 100, showOverflowTooltip: true }, option)
    this.addChild(column)
    return column
  }
  // 添加抽屉列
  useDrawerColumn(prop: string | TableColumnOption = {}) {
    const option: TableColumnOption = typeof prop === 'string' ? { prop } : prop
    const column = new ListPageTableColumn(option.prop)
    const onOpenDrawer = (row) => {
      this.dispatch('ListPage', 'openDrawer', row)
    }
    Object.assign(column._props, { minWidth: 100, onOpenDrawer }, option)
    this.addChild(column)
    column.setComponent(ListPageDrawerColumn)
    return column
  }

  private _authColumnIndex = 0
  // 添加权限列
  useAuthColumn(prop: string | TableColumnOption = {}) {
    const option: TableColumnOption = typeof prop === 'string' ? { prop } : prop
    const authColumn= this.add('ListPageAuthColumn', `ListPageAuthColumn${++this._authColumnIndex}`)
    authColumn._props.label="操作"
    authColumn._props.fixed = 'right'
    Object.assign(authColumn._props, option)
    return authColumn
  }
  addAuthColumn() {
    this.useAuthColumn()
    return this
  }

  end() {
    return this.upto('ListPage')
  }
}
